﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using SharePoint.Security.ContentScanner.Engine.Analyzers;
using SharePoint.Security.ContentScanner.Engine.Helpers;
using SharePoint.Security.DocHelpers.ExportFormats;

namespace SharePoint.Security.ContentScanner.Client.ViewModels
{
    class MainViewModel
    {
        public MainViewModel()
        {
            Errors = new List<ErrorInfo>();
        }

        public void GetData()
        {
            GetRegExPatterns();
            GetFileExtensions();
        }

        public List<SiteViewModel> Sites { get; set; }
        public BindingList<RegExViewModel> RegExes { get; set; }

        public BindingList<FileExtensionViewModel> FileExtensions { get; set; }

        //public List<ScanResult> Results { get; set; }

        public List<ErrorInfo> Errors { get; set; }

        public string CustomRegEx { get; set; }

        public bool IsRecovery { get; set; }

        public string ExcelFileName { get; set; }

        public int SuccessfullyScannedCount = 0;

        public int PreviousSelectedSites = 0;

        public int PreviousScannedSites = 0;

        public int TotalSitesSelectedForScan = 0;

        public void GetSites(List<string> urls, string type, bool fetchSubSites)
        {
            var logger = Logger.Log;
            try
            {
                if (urls == null || urls.Count == 0)
                {
                    Errors.Add(new ErrorInfo("Site collection URL(s) are required for scanning."));
                    return;
                }

                Errors.Clear();

                Sites = new List<SiteViewModel>();
                Helper.Urls.Clear();
                foreach (var site in Helper.GetSites(urls, Errors, type,false, fetchSubSites))
                {
                    if (!Sites.Exists(s => s.SitePath.Equals(site.ToString())))
                    {                 
                        Sites.Add(new SiteViewModel
                        {
                            IsSelected = true, SitePath = site
                        });
                    }
                }
            }
            catch (ArgumentException agex)
            {
                logger.Error("An error has occured: " + agex.Message);
                throw;
            }
        }

        private void GetRegExPatterns()
        {

            var content = File.ReadAllLines("RegExList.txt");
            RegExes = new BindingList<RegExViewModel>();

            foreach (var line in content)
            {
                var csvItem = line.Split(",".ToCharArray(), 2);
                string name;
                string regex;

                if (csvItem.Length==2)
                {
                    name = csvItem[0].Trim();
                    regex = csvItem[1].Trim();
                }
                else
                {
                    name = string.Empty;
                    regex = csvItem[0].Trim();
                }

                RegExes.Add(new RegExViewModel
                {
                    IsSelected = true,
                    Name = name,
                    Pattern = regex
                });
            }
        }

        private void GetFileExtensions()
        {
            FileExtensions = new BindingList<FileExtensionViewModel>();
            FileExtensions.Add(new FileExtensionViewModel
            {
                IsSelected = true,
                Name = "Word Document",
                FileExtension = ".docx"
            });            
            FileExtensions.Add(new FileExtensionViewModel
            {
                IsSelected = true,
                Name = "ASPX Page",
                FileExtension = ".aspx"
            });
        }

        public void Scan(List<string> sites, List<Pattern> regexs, List<SelectedFileExtension> fileExtns, string type)
        {
            Errors.Clear();
           
            var scanner = new Scanner();
            if (File.Exists(@"ScanFiles\SuccessfullyScannedSitesList.txt"))
            {
                File.Delete(@"ScanFiles\SuccessfullyScannedSitesList.txt");
            }
            scanner.totalSitesSelectedForScan = this.TotalSitesSelectedForScan;
            scanner.previousScannedSites = this.PreviousScannedSites;

            foreach (var site in sites)
            {
                scanner.Scan(site.Trim(), regexs, fileExtns, type, ExcelFileName);
                                                             
            }

            foreach (var item in scanner.Errors)
            {
                Errors.Add(item);
            }

            SuccessfullyScannedCount = scanner.sitesSuccessfullyScanned;
        }
    }

    public class RegExViewModel
    {
        public string Pattern { get; set; }

        public bool IsSelected { get; set; }

        public string Name { get; set; }
    }

    public class FileExtensionViewModel
    {
        public string FileExtension { get; set; }

        public bool IsSelected { get; set; }

        public string Name { get; set; }
    }
}
